﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Exam.Core.Model;

public partial class Manage_addQuestionCourse : ManagePageBase {
    protected int cid = 0;
    protected void Page_Load(object sender, EventArgs e) {
        Button1.Text = Resources.labels.add;
        Button2.Text = Resources.labels.back;

        if (Request.QueryString["cid"] == null) {
            Response.End();
        }
        if (!int.TryParse(Request.QueryString["cid"], out cid)) {
            Response.End();
        }
        else {
            if (!IsPostBack) {
                BindCourse();
                BindType();
                BindDiff();
                
            }
        }
    }

    private void BindType() {
        List<Exam.Core.Model.QuestionType> list = new Exam.Core.DAL.QuestionType().getQuestionTypeList();
        foreach (Exam.Core.Model.QuestionType obj in list) {
            QType.Items.Add(new ListItem(obj.QuestionTypeTitle.Trim(), obj.QuestionTypeId.Trim()));
        }
        QType.Items.Insert(0, new ListItem(Resources.labels.PleaseChkQType, "0"));
        QType.Attributes.Add("onchange", "showXuanxiang(this)");
    }

    private void BindDiff() {
        List<Exam.Core.Model.QuestionDifficulty> list = new Exam.Core.DAL.QuestionDifficulty().getQuestionDifficultyList();
        foreach (Exam.Core.Model.QuestionDifficulty obj in list) {
            QDifficulty.Items.Add(new ListItem(obj.QuestionDifficultyTitle.Trim(), obj.QuestionDifficultyId.Trim()));
        }
        QDifficulty.Items.Insert(0, new ListItem(Resources.labels.PleaseChkDiff, "0"));

    }


    private void BindCourse() {
        Exam.Core.Model.Course obj = new Exam.Core.DAL.Course().getCourseById(cid);
        cname.Text = obj.Title.Trim();
        StringBuilder sb = new StringBuilder();
        foreach (Knowledge know in obj.Knowledges) {
            sb.Append(know.Title.Trim());
            sb.Append("&nbsp;&nbsp;");
            QKnowledge.Items.Add(new ListItem(know.Title.Trim(), know.Id.Trim()));
        }
        QKnowledge.Items.Insert(0, new ListItem(Resources.labels.PleaseChkKnowledge, "0"));
        //cknow.Text = sb.ToString();


    }

    protected void Button1_Click(object sender, EventArgs e) {
        //增加试题
        string QType = this.QType.SelectedValue;
        string QDifficulty = this.QDifficulty.SelectedValue;
        string QKnowledge = this.QKnowledge.SelectedValue;
        string Qcontent = Server.HtmlEncode(this.Qcontent.Text.Trim());
        string QOptions = this.QOptions.Text.Trim();
        string QAnswer = this.QAnswer.Text.Trim();


        int questionid = new Exam.Core.DAL.Question().Add(Qcontent, QAnswer, QType, QDifficulty, QKnowledge, cid, "0");
        if (QType == "1" || QType == "2" || QType == "3") {
            string[] opts = System.Text.RegularExpressions.Regex.Split(QOptions, "\r\n");      //QOptions.Split();
            int result = new Exam.Core.DAL.Question().AddOpts(questionid, opts);
        }
        MessageBox.AlertThenRefresh(this.Page, Resources.labels.Add_ok);

    }

    protected void Button2_Click(object sender, EventArgs e) {
        Response.Redirect("Course.aspx?id=" + cid, true);
    }

}
